package com.leadnews.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.leadnews.mapper.LevelMapper;
import com.leadnews.pojo.Level;
import com.leadnews.pojo.User;
import com.leadnews.service.UserService;
import com.leadnews.vo.ResponseResult;
import io.jsonwebtoken.Claims;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

import java.io.Serializable;
import java.util.Map;

import static com.leadnews.utils.AppJwtUtil.getClaimsBody;

@RestController
@RequestMapping
@CrossOrigin(origins = "*",maxAge = 3600)
public class LoginController {
    @Autowired
    UserService userService;
    @PostMapping("/login")
    public ResponseResult login(@RequestBody User user){

        return userService.login(user);

    }
    @PostMapping("/loginMail")
    public ResponseResult loginMail(@RequestParam("mail") String mail){
        return  userService.loginMail(mail);
    }
    @PostMapping("/select")
    public ResponseResult select(@RequestBody Map<String,String> map){
        String token = map.get("token");
        ResponseResult result = new ResponseResult();
        result.setCode(0);
        Object id = null;
        try {
            Claims claimsBody = getClaimsBody(token);
//        int i = AppJwtUtil.verifyToken(claimsBody);
//        if(i<1){
//
            id = claimsBody.get("id");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("token过期无效");
        }
        System.out.println("解析token成功 ==> 用户的id值 == "+ id);
        User user = userService.getById((Serializable) id);
        if (user!=null){
            result.setCode(200);
            result.setData(user.getUsername());
//            result.setErrorMessage();
        }else{
            result.setErrorMessage("未登录或者登录已过期，请重新登陆");
        }
        System.out.println(result.getCode());
        return result;
    }
    @PostMapping("/iftoken")
    public ResponseResult iftoken(@RequestBody Map<String,String> map){
        String token = map.get("token");
        ResponseResult result = new ResponseResult();
        result.setCode(0);
        Object id = null;
        try {
            Claims claimsBody = getClaimsBody(token);
            id = claimsBody.get("id");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("token过期无效");
        }
        System.out.println("解析token成功 ==> 用户的id值 == "+ id);
        User user = userService.getById((Serializable) id);
        if (user!=null){
            result.setCode(200);
        }else{
            result.setErrorMessage("未登录或者登录已过期，请重新登陆");
        }
        System.out.println(result.getCode());
        return result;
    }
    @Autowired
    LevelMapper levelMapper;
    @PostMapping("/iflogin")
    public ResponseResult iflogin(@RequestParam("token") String token){

        ResponseResult result = new ResponseResult();
        result.setCode(0);
        Object id = null;
        try {
            Claims claimsBody = getClaimsBody(token);
            id = claimsBody.get("id");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("token过期无效");
        }
        System.out.println("解析token成功 ==> 用户的id值 == "+ id);
        User user = userService.getById((Serializable) id);
        if (user!=null){
            result.setCode(200);
            LambdaQueryWrapper<Level> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(Level::getUserId,user.getId());
            Level level = levelMapper.selectOne(queryWrapper1);
            result.setData(level);
        }else{
            result.setErrorMessage("未登录或者登录已过期，请重新登陆");
        }
        System.out.println(result.getCode());
        return result;
    }

}
